/*
-----------------------------------------------------------------------------
This file is part of NxGraphics, Multimedia Engine.
Author : Stephane Kyles. Developed in Prague, Czech Republic.
				_   __      _    __ _      __           
			   / | / /_  __| |  / /(_)____/ /___   ____ 
			  /  |/ /| |/_/| | / // // __  // _ \ / __ \
			 / /|  /_>  <  | |/ // // /_/ //  __// /_/ /
			/_/ |_//_/|_|  |___//_/ \__,_/ \___/ \____/ 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#ifndef __NXVIDEOURLCALLBACK_H__
#define __NXVIDEOURLCALLBACK_H__
 
#include "NxVideo_Prerequisites.h"

#include "../Dependencies/BERKELIUM/include/berkelium/Berkelium.hpp"
#include "../Dependencies/BERKELIUM/include/berkelium/Window.hpp"
#include "../Dependencies/BERKELIUM/include/berkelium/WindowDelegate.hpp"
#include "../Dependencies/BERKELIUM/include/berkelium/Context.hpp"
#pragma comment(lib , "../Dependencies/BERKELIUM/lib/berkelium.lib")
using namespace Berkelium;

namespace NxVideo_Namespace {

class NxVideo_URLCallback : public Berkelium::WindowDelegate
{

public :

	NxVideo_URLCallback()
	{
		isLoaded = false;
	}

	~NxVideo_URLCallback()
	{
	
	}

	void SetSize( int Width, int Height )
	{
		CurrentWidth = Width;
		CurrentHeight = Height;
		Buffer = new BYTE[ CurrentWidth * CurrentHeight * 4 ];
		for( int i = 0 ; i < CurrentWidth * CurrentHeight * 4; i++  )
		{
			Buffer[i] = 255;
		}
		isLoaded = true;
	}

	void onAddressBarChanged(Window *win, URLString newURL)
	{
		Log("onAddressBarChanged...");
	}
 
	void onStartLoading(Window *win, URLString newURL)
	{
		Log("loading...");
	}

	void onLoad(Window *win)
	{
	
	}
 
	void onCrashedWorker(Window *win) 
	{
	
	}

	void onCrashedPlugin( Window *win, WideString pluginName)
	{
	
	}

	void onProvisionalLoadError(Window *win, URLString url, int errorCode, bool isMainFrame)
	{
	
	}

	void onConsoleMessage(Window *win, WideString message, WideString sourceId, int line_no)
	{
	
	}

	void onScriptAlert(Window *win, WideString message, WideString defaultValue, URLString url, int flags, bool &success, WideString &value)
	{
	
	}

	void freeLastScriptAlert(WideString lastValue)
	{
	
	}

	void onNavigationRequested(Window *win, URLString newUrl, URLString referrer, bool isNewWindow, bool &cancelDefaultAction)
	{
	
	}

	void onLoadingStateChanged(Window *win, bool isLoading)
	{
	
	
	}
	
	void onTitleChanged( Window * win, WideString title)
	{
	
	}

	void onTooltipChanged(Window *win, WideString text) 
	{
	
	}


	void onCrashed( Window * win )
	{
		Log("onCrashed...");
	}

	void onUnresponsive( Window * win )
	{
	
	}

	void onResponsive(Window *win)
	{
	
	}

	void onExternalHost( Window *win, WideString message, URLString origin, URLString target) 
	{
	
	}

	void onCreatedWindow( Window * win, Window * newWindow, const Berkelium::Rect &initialRect )
	{
		Log("onCreatedWindow...");
	}

	void convertColors(const Berkelium::Rect &rect, const unsigned char *sourceBuffer)
	{
		int top = rect.top();
		int left = rect.left();
		for(int row = 0; row < rect.height(); row++)
		{
			for(int col = 0; col < rect.width(); col++)
			{
				Buffer[ ((top+row) * CurrentWidth + (left+col))*4 +0] = sourceBuffer[(row * rect.width() + col)*4 + 0]; 
				Buffer[((top+row) * CurrentWidth + (left+col))*4+1] = sourceBuffer[(row * rect.width() + col)*4 + 1]; 
				Buffer[((top+row) * CurrentWidth + (left+col))*4+2] = sourceBuffer[(row * rect.width() + col)*4 + 2]; 
				Buffer[((top+row) * CurrentWidth + (left+col))*4+3] = 255;//sourceBuffer[(row * rect.width() + col)*4 + 3]; 
			}
		}
	}

	void onPaint( Window *win, const unsigned char *sourceBuffer, const Berkelium::Rect &sourceBufferRect, size_t numCopyRects, const Berkelium::Rect *copyRects, int dx, int dy, const Berkelium::Rect &scrollRect) 
	{
		//Locker->lock();
		convertColors( sourceBufferRect, sourceBuffer );
		//Locker->unlock();
		return ;
	}

	unsigned char * GetBuffer()
	{
		return Buffer ;
	}

	void onWidgetCreated(Window *win, Widget *newWidget, int zIndex)
	{
	
	}

	void onWidgetDestroyed(Window *win, Widget *wid)
	{
	
	}

	void onWidgetResize( Window *win, Widget *wid, int newWidth, int newHeight) 
	{

	}

	void onWidgetMove( Window *win, Widget *wid, int newX, int newY)
	{
	
	}

	void onWidgetPaint( Window * win, Widget * wid, const unsigned char *sourceBuffer, const Berkelium::Rect &sourceBufferRect,
	size_t numCopyRects, const Berkelium::Rect *copyRects, int dx, int dy, const Berkelium::Rect &scrollRect)
	{
	
	}

	void onCursorUpdated(Window *win, const Berkelium::Cursor& newCursor)
	{

	}

	void onShowContextMenu( Window * win, const ContextMenuEventArgs& args) 
	{

	}

    std::string mURL;
	unsigned char * Buffer;
	int CurrentWidth;
	int CurrentHeight;
	bool isLoaded ;
	//boost::mutex * Locker; 
};

}//namespace

#endif